#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tornado.httpserver
import tornado.ioloop
import tornado.options
import tornado.web
import urls
import os
import socket
import time

def init_options():
    tornado.options.define('port', default=8000, type=int)
    tornado.options.define('worker', default=1, type=int)
    tornado.options.define('debug', default=False, type=bool)
    tornado.options.define('timeout', default=5, type=int)
    tornado.options.parse_command_line()

    return tornado.options.options

def timeoutcallback():
    print 'timeout!!!!!!'

_options = init_options()
socket.setdefaulttimeout(_options.timeout)

settings = {
    'debug': _options.debug,
    #'static_path': os.path.join(os.path.dirname(__file__), "/opt/web/Statis/web"),
    'static_path': os.path.join(os.path.dirname(__file__), "/home/leeray/workspaces/Python/wanalytics/Statis/web"),
    #'static_url_prefix': '/static/'
}

application = tornado.web.Application(urls.urls, **settings)

if __name__ == "__main__":
    _httpserver = tornado.httpserver.HTTPServer(application)
    _httpserver.bind(_options.port)
    _httpserver.start(_options.worker)
    io_loop = tornado.ioloop.IOLoop.instance()
    #io_loop.add_timeout(time.time()+5, timeoutcallback)
    io_loop.start()

